<?php

namespace app\controller;

use app\BaseController;
use app\model\Archives;
use app\model\Contract;
use app\model\Invoice;
use app\model\Parameter;

/**
 * 发票管理
 * Class Invoice
 * @package app\controller
 * @author 下雨吧จุ๊บ
 */
class InvoiceController extends BaseController
{
    /**
     * @apiGroup 发票管理
     * @apiVersion 1.0.0
     * @api {GET} /invoice  发票管理列表
     * @apiDescription 发票管理列表
     * @apiParam {String} listRows 每页数量(必填*)
     * @apiParam {String} page 当前页数(必填*)
     * @apiSuccessExample SuccessExample 成功事例
     * HTTP/1.0.0 200
     * {
     * status: 1,
     * data: 发票管理列表,
     * message: '获取发票管理列表成功'
     * }
     * @apiErrorExample ErrorExample 失败选项
     * HTTP/1.0.0 500
     * {
     * status: '0',
     * message: '错误提示'
     * }
     */
    public function index()
    {
        $data = input();
        if (empty($data['listRows'])) {
            $data['listRows'] = '8';
        }
        if (empty($data['page'])) {
            $data['page'] = '1';
        }
        $map[] = ['p.title','like','%%'];
        if (!empty($data['map']['title'])){
            $map[] = ['p.title','like','%'. $data['map']['title'] .'%'];
        }
        if (empty($data['archivesid'])){
            //左侧菜单
            $data['list'] = Invoice::alias('i')
                ->join(['parameter' => 'p'], 'p.id=i.invoicetypeid')
//                ->join(['user' => 'u1'], 'u1.id=i.makeuserid')
//                ->join(['user' => 'u2'], 'u2.id=i.createuserid')
//                ->field('i.*,u1.realname as makeuser,u2.realname as createuser,p.title as invoicetypetitle')
                ->where($map)
                ->field('i.*,p.title as invoicetypetitle')
                ->paginate([
                    'list_rows' => $data['listRows'],
                    'page' => $data['page'],
                ]);
        }else{
            //客户档案中
            $data['list'] = Invoice::alias('i')
                ->join(['parameter' => 'p'], 'p.id=i.invoicetypeid')
//                ->join(['user' => 'u1'], 'u1.id=i.makeuserid')
//                ->join(['user' => 'u2'], 'u2.id=i.createuserid')
                ->where('i.archivesid',$data['archivesid'])
//                ->field('i.*,u1.realname as makeuser,u2.realname as createuser,p.title as invoicetypetitle')
                ->field('i.*,p.title as invoicetypetitle')
                ->paginate([
                    'list_rows' => $data['listRows'],
                    'page' => $data['page'],
                ]);
        }
        success($data, '获取发票管理列表成功');
    }

    /**
     * @apiGroup 发票管理
     * @apiVersion 1.0.0
     * @api {GET} /invoice/create  新增
     * @apiDescription 新增界面
     * @apiSuccessExample SuccessExample 成功事例
     * HTTP/1.0.0 200
     * {
     * status: 1,
     * data: 跟进阶段列表,
     * message: '获取数据成功'
     * }
     * @apiErrorExample ErrorExample 失败选项
     * HTTP/1.0.0 500
     * {
     * status: '0',
     * message: '错误提示'
     * }
     */
    public function create()
    {
        //客户档案列表
        $data['archives'] = Archives::select();
        //获取合同编号
        $data['code'] = Contract::where('status',1)->field('code,id')->select();
        //获取发票类型列表
        $data['paytype'] = Parameter::where('status', 1)->where('pid', 84)->select();
        //获取业务人员列表
        $data['user'] = \app\model\User::where('status', 1)->select();
        success($data, '获取数据成功');
    }

    /**
     * @apiGroup 发票管理
     * @apiVersion 1.0.0
     * @api {POST} /invoice  新增保存
     * @apiDescription 新增保存
     * @apiParam {String} archivesid 客户档案id
     * @apiParam {String} contractid 合同id(必填*)
     * @apiParam {String} customer 客户名称(必填*)
     * @apiParam {String} invoicetypeid 发票类型id(必填*)
     * @apiParam {String} invoiceprice 发票金额(必填*)
     * @apiParam {String} makeuserid 开票人id(必填*)
     * @apiParam {String} maketime 开票时间(必填*)
     * @apiParam {String} ismake 是否开发票(必填*)
     * @apiParam {String} createuserid 录入人id(必填*)
     * @apiParam {String} content 开票备注
     * @apiParam {String} status 状态
     * @apiSuccessExample SuccessExample 成功事例
     * HTTP/1.0.0 200
     * {
     * status: 1,
     * message: '新增成功'
     * }
     * @apiErrorExample ErrorExample 失败选项
     * HTTP/1.0.0 500
     * {
     * status: '0',
     * message: '错误提示'
     * }
     */
    public function save()
    {
        $data = input();
        //必填项验证
        if (empty($data['contractid'])) {
            error('请先选择合同');
        }
        //必填项验证
        if (empty($data['invoicetypeid'])) {
            error('请选择发票类型');
        }
        //根据合同id补充客户档案id
        $data['archivesid'] = Contract::where('id',$data['contractid'])->find()['archivesid'];
        //补充时间
        $data['createtime'] = time();
        $data['updatetime'] = time();
        //添加数据
        $result = Invoice::insert($data);
        if ($result) {
            success(array(), '新增成功');
        }
        error('新增失败');
    }

    /**
     * @apiGroup 发票管理
     * @apiVersion 1.0.0
     * @api {GET} /invoice/:id  编辑读取
     * @apiDescription 编辑页面(:id是指当前id,直接写在路由中)
     * @apiSuccessExample SuccessExample 成功事例
     * HTTP/1.0.0 200
     * {
     * status: 1,
     * data: array
     * message: '获取数据成功'
     * }
     * @apiErrorExample ErrorExample 失败选项
     * HTTP/1.0.0 500
     * {
     * status: '0',
     * message: '错误提示'
     * }
     */
    public function read()
    {
        $data = input();
        //必填项验证
        if (empty($data['id'])) {
            error('520数据错误(Missing parameter id)');
        }
        //客户档案列表
        $data['archives'] = Archives::select();
        //获取合同编号
        $data['code'] = Contract::where('status',1)->field('code,id')->select();
        //获取发票类型列表
        $data['paytype'] = Parameter::where('status', 1)->where('pid', 84)->select();
        //获取业务人员列表
        $data['user'] = \app\model\User::where('status', 1)->select();
        //返回数据详情
        $data['info'] = Invoice::alias('i')
            ->join(['contract' => 'c'], 'c.id=i.contractid')
            ->where('i.id', $data['id'])
            ->field('i.*,c.code')
            ->find();
        //补充订单金额
        $data['info']['contractprice'] = Contract::where('id',$data['info']['contractid'])->find()['contractprice'];
        success($data, '获取数据成功');
    }

    /**
     * @apiGroup 发票管理
     * @apiVersion 1.0.0
     * @api {PUT} /invoice/:id  编辑保存
     * @apiParam {String} contractid 合同id(必填*)
     * @apiParam {String} customer 客户名称(必填*)
     * @apiParam {String} invoicetypeid 发票类型id(必填*)
     * @apiParam {String} invoiceprice 发票金额(必填*)
     * @apiParam {String} makeuserid 开票人id(必填*)
     * @apiParam {String} maketime 开票时间(必填*)
     * @apiParam {String} ismake 是否开发票(必填*)
     * @apiParam {String} createuserid 录入人id(必填*)
     * @apiParam {String} content 开票备注
     * @apiParam {String} status 状态
     * @apiSuccessExample SuccessExample 成功事例
     * HTTP/1.0.0 200
     * {
     * status: 1,
     * message: '保存成功'
     * }
     * @apiErrorExample ErrorExample 失败选项
     * HTTP/1.0.0 500
     * {
     * status: '0',
     * message: '错误提示'
     * }
     */
    public function update()
    {
        $data = input();
        //必填项验证
        if (empty($data['id'])) {
            error('520数据错误(Missing parameter id)');
        }
        //必填项验证
        if (empty($data['contractid'])) {
            error('请先选择合同');
        }
        //必填项验证
        if (empty($data['invoicetypeid'])) {
            error('请选择发票类型');
        }
        //根据合同id补充客户档案id
        $data['archivesid'] = Contract::where('id',$data['contractid'])->find()['archivesid'];
        //更新修改时间
        $data['updatetime'] = time();
        //更新数据
        $result = Invoice::where('id', $data['id'])->save($data);
        success(array(), '保存成功');
    }

    /**
     * @apiGroup 发票管理
     * @apiVersion 1.0.0
     * @api {DELETE} /invoice/0  删除
     * @apiDescription 删除(路由中直接写0,ids集合使用参数传递)
     * @apiParam {array} ids id集合
     * @apiSuccessExample SuccessExample 成功事例
     * HTTP/1.0.0 200
     * {
     * status: 1,
     * message: '删除成功'
     * }
     * @apiErrorExample ErrorExample 失败选项
     * HTTP/1.0.0 500
     * {
     * status: '0',
     * message: '错误提示'
     * }
     */
    public function delete()
    {
        $data = input();
        //必填项验证
        if (empty($data['ids'])) {
            error('520数据错误(Missing parameter id)');
        }
        $result = Invoice::whereIn('id', $data['ids'])->delete();
        success(array(), '删除成功');
    }

    /**
     * @apiGroup 发票管理
     * @apiVersion 1.0.0
     * @api {GET} /invoice/:id/edit  选择合同编号请求
     * @apiDescription 读取数据(:id是指当前id,直接写在路由中)
     * @apiSuccessExample SuccessExample 成功事例
     * HTTP/1.0.0 200
     * {
     * status: 1,
     * message: '获取数据成功'
     * }
     * @apiErrorExample ErrorExample 失败选项
     * HTTP/1.0.0 500
     * {
     * status: '0',
     * message: '错误提示'
     * }
     */
    public function edit()
    {
        $data = input();
        //必填项验证
        if (empty($data['id'])) {
            error('520数据错误(Missing parameter id)');
        }
        //根据合同id获取合同详情
        $data['info'] = Contract::where('id',$data['id'])->find();
        success($data,'获取数据成功');
    }
}
